package cn.edu.sgu.www.mhxysy.controller.role;

import cn.edu.sgu.www.mhxysy.consts.CustomConstants;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.consts.MimeTypes;
import cn.edu.sgu.www.mhxysy.dto.role.RoleAccountDTO;
import cn.edu.sgu.www.mhxysy.entity.role.RoleAccount;
import cn.edu.sgu.www.mhxysy.function.computer.score.impl.RoleScoreComputer;
import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.restful.JsonPage;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.role.RoleAccountService;
import cn.edu.sgu.www.mhxysy.vo.role.RoleAccountVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotBlank;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "角色账号管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/role_account", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class RoleAccountController {

	private final RoleScoreComputer roleScoreComputer;
	private final RoleAccountService roleAccountService;

	@Autowired
	public RoleAccountController(
			RoleScoreComputer roleScoreComputer,
			RoleAccountService roleAccountService) {
		this.roleScoreComputer = roleScoreComputer;
		this.roleAccountService = roleAccountService;
	}

	@ApiOperation("计算角色评分")
	@RequestMapping(value = "/compute", method = RequestMethod.GET)
	public JsonResult<Integer> compute(
			@ApiParam(name = "roleId", value = "角色ID", required = true)
			@NotBlank(message = CustomMessages.ROLE_ID_NOT_NULL) String roleId) {
		Integer score = roleScoreComputer.compute(roleId);

		return JsonResult.success(null, score);
	}

	@ApiOperation("导出数据")
	@RequestMapping(value = "/exportData", method = RequestMethod.GET)
	public void exportData() throws UnsupportedEncodingException {
		roleAccountService.exportData();
	}

	@ApiOperation("数据导入")
	@RequestMapping(value = "/importData", method = RequestMethod.POST)
	public JsonResult<Void> importData(MultipartFile file) {
		roleAccountService.importData(file);

		return JsonResult.success("导入成功");
	}

	@ApiOperation("添加角色")
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public JsonResult<Void> insert(@Validated RoleAccountDTO roleAccountDTO) {
		roleAccountService.insert(roleAccountDTO);

		return JsonResult.success(CustomMessages.INSERT_SUCCESS);
	}

	@ApiOperation("通过ID批量删除角色")
	@RequestMapping(value = "/deleteByIds", method = RequestMethod.POST)
	public JsonResult<Void> deleteByIds(
			@ApiParam(name = "id", value = "角色ID列表", required = true)
			@RequestParam("ids") List<String> ids) {
		roleAccountService.deleteByIds(ids);

		return JsonResult.success(CustomMessages.DELETE_SUCCESS);
	}

	@ApiOperation("通过ID修改角色信息")
	@RequestMapping(value = "/updateById", method = RequestMethod.POST)
	public JsonResult<Void> updateById(@Validated RoleAccountDTO roleAccountDTO) {
		roleAccountService.updateById(roleAccountDTO);

		return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
	}

	@ApiOperation("查询全部角色")
	@RequestMapping(value = "/selectAll", method = RequestMethod.GET)
	public JsonResult<List<RoleAccount>> selectAll() {
		List<RoleAccount> list = roleAccountService.selectAll();

		return JsonResult.success(null, list);
	}

	@ApiOperation("通过ID查询角色信息")
	@RequestMapping(value = "/selectById", method = RequestMethod.GET)
	public JsonResult<RoleAccount> selectById(
			@ApiParam(name = "id", value = "角色ID", required = true)
			@NotBlank(message = CustomMessages.ID_NOT_NULL) String id) {
		RoleAccount roleAccount = roleAccountService.selectById(id);

		return JsonResult.success(null, roleAccount);
	}

	@ApiOperation("通过ID查询角色详情")
	@RequestMapping(value = "/selectRoleDetails", method = RequestMethod.GET)
	public JsonResult<RoleAccountVO> selectRoleDetails(
			@ApiParam(name = "id", value = "角色ID", required = true)
			@NotBlank(message = CustomMessages.ID_NOT_NULL) String id) {
		RoleAccountVO roleAccountVO = roleAccountService.selectRoleDetails(id);

		return JsonResult.success(null, roleAccountVO);
	}

	@ApiOperation("分页查询角色列表")
	@RequestMapping(value = "/selectByPage", method = RequestMethod.POST)
	public JsonResult<JsonPage<RoleAccount>> selectByPage(Pager<RoleAccount> pager) {
		Page<RoleAccount> page = roleAccountService.selectByPage(pager);

		return JsonResult.restPage(page);
	}

}